package org.jsbd.boss.service.impl;

import java.util.HashMap;
import java.util.List;

import org.jsbd.boss.common.IpTableHolder;
import org.jsbd.boss.dao.IIpDAO;
import org.jsbd.boss.domian.ProvinceInfo;
import org.jsbd.boss.domian.TPhoneAreaProvinceInfo;
import org.jsbd.boss.service.IIpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class IpServiceImpl implements IIpService {

    private IIpDAO ipDAO;
    private static final Logger logger = LoggerFactory.getLogger(IpServiceImpl.class);

    private static HashMap<Integer, String> map = new HashMap<Integer, String>();

    static {
        map.put(-1, "全国");
        map.put(0, "北京");
        map.put(1, "天津");
        map.put(2, "上海");
        map.put(3, "重庆");
        map.put(4, "黑龙江");
        map.put(5, "吉林");
        map.put(6, "辽宁");
        map.put(7, "内蒙古");
        map.put(8, "宁夏");
        map.put(9, "甘肃");
        map.put(10, "新疆");
        map.put(11, "陕西");
        map.put(12, "山西");
        map.put(13, "山东");
        map.put(14, "河北");
        map.put(15, "河南");
        map.put(16, "西藏");
        map.put(17, "云南");
        map.put(18, "四川");
        map.put(19, "贵州");
        map.put(20, "广西");
        map.put(21, "广东");
        map.put(22, "福建");
        map.put(23, "湖南");
        map.put(24, "湖北");
        map.put(25, "江西");
        map.put(26, "浙江");
        map.put(27, "江苏");
        map.put(28, "安徽");
        map.put(29, "青海");
        map.put(30, "海南");
    }

    @Override
    public TPhoneAreaProvinceInfo getUserLocationByPhoneArea(String phoneArea) {
        List<TPhoneAreaProvinceInfo> phoneAreaList = IpTableHolder.getInstance().getmPhoneAreaList();
        TPhoneAreaProvinceInfo phoneAreaInfo = null;
        if (phoneAreaList == null || phoneAreaList.isEmpty()) {
            return null;
        }
        for (TPhoneAreaProvinceInfo info : phoneAreaList) {
            if (info.getPhoneNumber().equals(phoneArea)) {
                phoneAreaInfo = info;
                break;
            }
        }
        return phoneAreaInfo;
    }


    @Override
    public String getProvinceName(Integer provinceId) {
        return map.get(provinceId);
    }

    public void getAllIpInfo() {
        logger.info("load phone area info from ip database.");
//        IpTableHolder.getInstance().loadIpToMemory(ipDAO.getAllIpInfo());
//        IpTableHolder.getInstance().loadPhoneAreaToMemory(ipDAO.getAllPhoneAreaProvinceInfo());
    }

    public IIpDAO getIpDAO() {
        return ipDAO;
    }

    public void setIpDAO(IIpDAO ipDAO) {
        this.ipDAO = ipDAO;
    }

    @Override
    public List<ProvinceInfo> getProvinceInfoList() {
        return ipDAO.getAllprovinceInfo();
    }

}
